#include "Brunnen.h"

Brunnen::Brunnen(const string* path, int start){


	modelKoerper = new Geode;
	modelSaeule = new Geode;
	modelSpitze = new Geode;

	flaecheOben = new MatrixTransform;
	flaecheMitte = new MatrixTransform;
	flaecheUnten = new MatrixTransform;
	transformSaeule = new MatrixTransform;
	transformSpitze = new MatrixTransform;

	brunnen = new MatrixTransform;
	polygonMode = new PolygonMode;

	erstellen();
	bauen(path, start);
	transformieren();
	zusammenBauen();

}

/** Erstellen der einzelnen Teile **/
void Brunnen::erstellen(){

	koerper = new Rotationskoerper(6,true,&Funktionen::funktionSchaleGewoelbt);
	saeule = new Rotationskoerper(35,false,&Funktionen::funktionSaeule);
	spitze = new Rotationskoerper(2,false,&Funktionen::funktionKegel);

}

/** Bauen des Brunnens **/
void Brunnen::bauen( const string* path, int start ){

	modelKoerper->addDrawable(koerper->get());
	
	/** Setzen der Textur **/
	ref_ptr<Texture2D> texture1 = new Texture2D ;
	ref_ptr<osg::Image> image1 = osgDB::readImageFile( path[start] );
	texture1->setImage(image1.get());

	texture1->setWrap(Texture::WRAP_S, Texture::MIRROR);
	texture1->setWrap(Texture::WRAP_T, Texture::MIRROR);

	modelKoerper->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture1.get());

    modelSaeule->addDrawable(saeule->get());
	ref_ptr<Texture2D> texture2 = new Texture2D ;
	ref_ptr<osg::Image> image2 = osgDB::readImageFile( path[start+1] );
	texture2->setImage(image2.get());

	texture2->setWrap(Texture::WRAP_S, Texture::MIRROR);
	texture2->setWrap(Texture::WRAP_T, Texture::MIRROR);

	modelSaeule->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture2.get());

    modelSpitze->addDrawable(spitze->get());
	ref_ptr<Texture2D> texture3 = new Texture2D ;
	ref_ptr<osg::Image> image3 = osgDB::readImageFile( path[start+2] );
	texture3->setImage(image3.get());

	texture3->setWrap(Texture::WRAP_S, Texture::MIRROR);
	texture3->setWrap(Texture::WRAP_T, Texture::MIRROR);

	modelSpitze->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture3.get());

	delete koerper;
	delete saeule;
	delete spitze;

}

/** Transformieren der Elemente **/
void Brunnen::transformieren(){

	flaecheOben->setMatrix(Matrix::scale(1.0f, 1.0f, 1.0f) * Matrix::translate(0.0f, 0.0f, 0.0f));
	flaecheOben->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,StateAttribute::ON);
	flaecheOben->addChild(modelKoerper.get());

	flaecheMitte->setMatrix(Matrix::scale(1.5f, 1.5f, 1.5f) * Matrix::translate(0.0f, 0.0f, -10.0f));
	flaecheMitte->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,StateAttribute::ON);
	flaecheMitte->addChild(modelKoerper.get());

	flaecheUnten->setMatrix(Matrix::scale(2.0f, 2.0f, 2.0f) * Matrix::translate(0.0f, 0.0f, -20.0f));
	flaecheUnten->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,StateAttribute::ON);
	flaecheUnten->addChild(modelKoerper.get());

	transformSaeule->setMatrix(Matrix::scale(1.0f, 1.0f, 1.0f) * Matrix::translate(0.0f, 0.0f, -29.0f));
	transformSaeule->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,StateAttribute::ON);
	transformSaeule->addChild(modelSaeule.get());

	transformSpitze->setMatrix(Matrix::scale(1.0f, 1.0f, -1.0f) * Matrix::translate(0.0f, 0.0f, 6.6f));
	transformSpitze->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,StateAttribute::ON);
	transformSpitze->addChild(modelSpitze.get());

}

/** Zusammenbauen des Brunnens **/
void Brunnen::zusammenBauen(){

	brunnen->setMatrix(Matrix::rotate(PI_2,Vec3(0,0,0)));
    brunnen->addChild( flaecheOben.get() );
	brunnen->addChild( flaecheMitte.get() );
	brunnen->addChild( flaecheUnten.get() );
	brunnen->addChild( transformSaeule.get() );
	brunnen->addChild( transformSpitze.get() );
	brunnen->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL, StateAttribute::ON);


    polygonMode->setMode(PolygonMode::FRONT_AND_BACK, PolygonMode::LINE);
}

/** zurueck geben des Brunnens **/
ref_ptr<MatrixTransform> Brunnen::get(){
	return brunnen.release();
}

/** Freigeben der Resourcen **/
Brunnen::~Brunnen(){
	free(koerper);
	free(saeule);
	free(spitze);
	free(modelKoerper);
	free(modelSaeule);
	free(modelSpitze);
	free(flaecheOben);
	free(flaecheMitte);
	free(flaecheUnten);
	free(transformSaeule);
	free(transformSpitze);
	free(brunnen);
	free(polygonMode);
}
